package com.hk.jmm.app;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.InputType;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.hk.jmm.R;
import com.hk.jmm.model.LoginByPasswordModel;
import com.hk.jmm.presenter.LoginByPasswordPresenter;
import com.luck.picture.lib.utils.ToastUtils;
import com.skyz.base.activity.BaseMvpActivity;
import com.skyz.wrap.utils.CountDownTimerUtils;

import java.util.List;

public class LoginActivity extends BaseMvpActivity<LoginByPasswordModel, LoginActivity, LoginByPasswordPresenter> {
    public static void showActivity(Activity activity) {
        Intent intent = new Intent(activity, LoginActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onMessageEvent(Object event) {

    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }


    public EditText et_account,et_pass;
    public EditText et_phone,et_code;
    TextView pass_tv,code_tv,tv_agreement;
    View pass_icon,code_icon;

    View layout_account,layout_pass,layout_phone,layout_code;

    public TextView click_send_code;

    public ImageView sys_btn_show;

    private boolean isPassLogin = true;

    public boolean getPassLogin(){
        return isPassLogin;
    }


    @Override
    protected void onLayoutInflated() {
        setLayoutFullScreen(true);
        pass_tv = findViewById(R.id.pass_tv);
        code_tv = findViewById(R.id.code_tv);
        pass_icon = findViewById(R.id.pass_icon);
        code_icon = findViewById(R.id.code_icon);

        layout_account = findViewById(R.id.layout_account);
        layout_pass = findViewById(R.id.layout_pass);
        layout_phone = findViewById(R.id.layout_phone);
        layout_code = findViewById(R.id.layout_code);

        et_account = findViewById(R.id.et_account);
        et_pass = findViewById(R.id.et_pass);
        et_phone = findViewById(R.id.et_phone);
        et_code = findViewById(R.id.et_code);

        sys_btn_show = findViewById(R.id.sys_btn_show);

        click_send_code= findViewById(R.id.click_send_code);

        tv_agreement= findViewById(R.id.tv_agreement);

    }


    @Override
    protected void initData(Bundle savedInstanceState) {
        findViewById(R.id.click_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        pass_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateUIByTabIsPass(true);
            }
        });

        code_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateUIByTabIsPass(false);
            }
        });

        pass_tv.performClick();

        String str = "登录代表您已同意《隐私政策》";
        SpannableString spannableString = getSpannableString(str);
        tv_agreement.setMovementMethod(LinkMovementMethod.getInstance());
        tv_agreement.setText(spannableString);


        findViewById(R.id.click_forgot).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ForgotActivity.showActivity(LoginActivity.this);
            }
        });
        findViewById(R.id.click_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getMvpPresenter().login();
            }
        });

        sys_btn_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getMvpPresenter().sysShow();

            }
        });

        click_send_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getMvpPresenter().sendCode();

            }
        });



    }

    private void updateUIByTabIsPass(boolean isPassLogin) {
        this.isPassLogin = isPassLogin;
        pass_icon.setVisibility(isPassLogin?View.VISIBLE:View.GONE);
        code_icon.setVisibility(isPassLogin?View.GONE:View.VISIBLE);

        pass_tv.setTypeface(null,isPassLogin?Typeface.BOLD:Typeface.NORMAL);
        code_tv.setTypeface(null,isPassLogin?Typeface.NORMAL:Typeface.BOLD);

        layout_account.setVisibility(isPassLogin?View.VISIBLE:View.GONE);
        layout_pass.setVisibility(isPassLogin?View.VISIBLE:View.GONE);

        layout_phone.setVisibility(isPassLogin?View.GONE:View.VISIBLE);
        layout_code.setVisibility(isPassLogin?View.GONE:View.VISIBLE);



    }

    private  SpannableString getSpannableString(String str) {
        SpannableString spannableString = new SpannableString(str);
        ClickableSpan clickableSpanYinsi = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                ToastUtils.showToast(widget.getContext(), "隐私政策");
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.parseColor("#FFF03550"));
                ds.setUnderlineText(false);
            }
        };
        int startB = str.indexOf("《隐私政策》");
        spannableString.setSpan(clickableSpanYinsi, startB, startB+6, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        return spannableString;
    }


    @Override
    protected LoginByPasswordPresenter initMvpPresenter() {
        return new LoginByPasswordPresenter(this, getLifecycle());
    }

    @Override
    protected void onDestroy() {
        getMvpPresenter().onDestroy();
        super.onDestroy();

    }
}
